import {
  GenWebType,
  batchGenCode,
  deleteCodeByIds,
  fetchCodeList,
  synchDb,
} from '@/api/modules/tool/gen/gen';
import { response } from '@/utils/table/table';
import { codeTable, genInterface } from './table';
/**
 * @description: 加载代码列表
 */
export const codeList = async () => {
  codeTable.value.loading = true;
  const { data } = await fetchCodeList(codeTable.value.query);
  codeTable.value.data = data.rows;
  codeTable.value.query.total = data.total;
  codeTable.value.loading = false;
};

/**
 * @description: 编辑表信息
 * @param {string} id
 */
export const editTable = async (id?: number) => {
  codeTable.value.keys = [id!];
  genInterface.value = 'CONFIG';
};

/**
 * @description: 删除单张表
 * @param {string} name
 */
export const deleteTable = async (name: string) => {
  await response(deleteCodeByIds, name);
  codeTable.value.keys = [];
  codeList();
};

/**
 * @description: 下载代码
 */
export const downloadCode = (type: GenWebType) => {
  batchGenCode(codeTable.value.keys as any, type);
};

/**
 * @description: 打开导入数据库的窗口
 */
export const importDb = () => {
  genInterface.value = 'IMPORT_DB';
};

/**
 * @description: 同步数据库
 * @param {string} tableName 数据表名
 */
export const asyncTable = async (tableName: string) => {
  await response(synchDb, tableName);
  codeList();
};
